package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.SOrderPayRoleEnum;
import cn.huiyunche.base.service.enums.UserTypeEnum;
import cn.huiyunche.base.service.interfaces.SOrderPayRoleService;
import cn.huiyunche.base.service.interfaces.UserService;
import cn.huiyunche.base.service.mappers.SOrderPayroleMapper;
import cn.huiyunche.base.service.model.SOrderPayrole;
import cn.huiyunche.base.service.model.SOrderPayroleExample;
import cn.huiyunche.base.service.model.SUser;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单 支付角色关联Service
 *
 * @author lm
 */
@Service
public class SOrderPayRoleServiceImpl implements SOrderPayRoleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SOrderPayRoleServiceImpl.class);

    @Autowired
    private UserService userService = null;

    @Autowired
    private SOrderPayroleMapper sOrderPayroleMapper = null;

    private SOrderPayroleMapper getSOrderPayroleMapper() {
        return this.sOrderPayroleMapper;
    }

    private UserService getUserService() {
        return this.userService;
    }

    @Override
    public Result<String> addSorderPayRoleRelations(Long orderId, Long userId, Integer payRole, String phone) {
        LOGGER.info("add sorder payrole relations params orderId : {}, userId : {}, payRole : {}, phone : {}.", orderId,
                userId, payRole, phone);
        Result<String> result = new Result<>(true, "订单支付角色关联完成");
        if (payRole == null || payRole == 0) {
            payRole = 10;
        } else if (payRole == SOrderPayRoleEnum.OTHERPAY.getValue() && StringUtils.isEmpty(phone)) {
            result.setSuccess(false);
            result.setMessage("商户手机号不能为空！");
            LOGGER.info(
                    "add sorder payrole relations params orderId : {}, userId : {}, payRole : {}, phone : {}, phone is null return.",
                    orderId, userId, payRole, phone);
            return result;
        }
        SOrderPayrole sOrderPayrole = new SOrderPayrole();
        sOrderPayrole.setOrderId(orderId);
        sOrderPayrole.setPayRole(payRole);
        sOrderPayrole.setUserType(UserTypeEnum.SCOMPANY.getValue());
        if (payRole == SOrderPayRoleEnum.MYSELFPAY.getValue()) {
            SUser user = this.getUserService().getById(userId);
            phone = user.getPhone();
            LOGGER.info("add sorder payrole relations params orderId : {}, userId : {}, payRole : {}, phone : {}, user : {}.", orderId,
                    userId, payRole, phone, user);
        } else {
            SUser user = this.getUserService().getByPhone(phone, UserTypeEnum.SCOMPANY.getValue());
            userId = user.getId();
            LOGGER.info("add sorder payrole relations params orderId : {}, userId : {}, payRole : {}, phone : {}, user : {}.", orderId,
                    userId, payRole, phone, user);
        }
        sOrderPayrole.setPhone(phone);
        sOrderPayrole.setUserId(userId);
        LOGGER.info("add sorder payrole relations params orderId : {}, sOrderPayrole : {}.", orderId, sOrderPayrole);
        this.getSOrderPayroleMapper().insertSelective(sOrderPayrole);
        return result;
    }

    @Override
    public SOrderPayrole getUserByOrderId(Long id) {
        LOGGER.info("select sorder payRole by orderId params id : {}.", id);
        try {
            SOrderPayroleExample example = new SOrderPayroleExample();
            example.createCriteria().andOrderIdEqualTo(id);
            List<SOrderPayrole> sOrderPayroles = this.getSOrderPayroleMapper().selectByExample(example);
            if (CollectionUtils.isNotEmpty(sOrderPayroles)) {
                LOGGER.info("select sorder payRole by orderId params id : {}, sOrderPayroles.get(0) : {}.", id, sOrderPayroles.get(0));
                return sOrderPayroles.get(0);
            }
        } catch (Exception e) {
            LOGGER.info("select sorder payRole by orderId params id : {}, error : {}.", id, e);
            throw new BusinessException("根据业务ID查询支付角色关系异常");
        }
        return null;
    }

}